package com.reggie.service.impl;

import com.reggie.dao.DishFlavorDao;
import com.reggie.dto.DishDto;
import com.reggie.pojo.DishFlavor;
import com.reggie.service.DishFlavorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishFlavorServiceImpl implements DishFlavorService {
    @Autowired
    private DishFlavorDao dao;

    @Override
    public void save(DishDto dishDto) {
        for (DishFlavor flavor : dishDto.getFlavors()) {
            dao.insert(flavor);
        }
    }

    @Override
    public List<DishFlavor> selectByDishId(Long id) {
        return dao.selectByDishId(id);
    }

    @Override
    public void updateDishFlavor(List<DishFlavor> dishFlavors,Long DishId) {
        System.out.println(dishFlavors);
        //进行修改之前先将里面所有的味道都清空
        //dishFlavors是空的说明前端将口味全都删除了所有后端也要同步删除
        //无论什么情况都要先删除味道表中的数据，如果有数据就会添加没有就拉到
        dao.deleteByDishId(DishId);
        //清空后按照里面的味道逐个增加进去
        dishFlavors.forEach(item -> dao.insert(item));
    }

    @Override
    public void deleteByDishIds(Long... ids) {
        for (Long id : ids) {
            if(id != null) {
                dao.deleteByDishId(id);
            }
        }
    }
}
